package com.treeman.controller.user;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.treeman.api.controller.marker.CurrentUserMarkerDataController;
import com.treeman.user.User;
import com.treeman.user.exception.UserNotFoundException;
import com.treeman.user.service.UserService;

@Controller
public class UserProfileController {

	@Autowired
	private UserService userService;

	private static Logger log = Logger
			.getLogger(CurrentUserMarkerDataController.class);

	@PreAuthorize("isAuthenticated()")
	@RequestMapping(value = "/user/{username}", method = RequestMethod.GET)
	public ModelAndView showUserByUsername(@PathVariable String username) {
		log.debug("Getting user by username: " + username);

		User selectedUser;
		try {
			selectedUser = userService.getUserByUsername(username);
			return new ModelAndView("user/profile", "selectedUser",
					selectedUser);
		} catch (UserNotFoundException ex) {
			log.debug("User " + username + " NOT FOUND! ");
		}

		return new ModelAndView("user/profile", "selectedUser", null);
	}
}
